package com.a3dweijing.wetouch2.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.network.RetrofitUtil;
import com.a3dweijing.wetouch2.adapter.MarKetAdapter;
import com.a3dweijing.wetouch2.base.BaseActivity;
import com.a3dweijing.wetouch2.bean.MarketDate;
import com.a3dweijing.wetouch2.bean.MarketItem;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MarKetActivity extends BaseActivity
{
    
    @BindView(R.id.top_text)
    TextView topText;
    
    @BindView(R.id.top_right)
    TextView topRight;
    
    @BindView(R.id.top_all)
    RelativeLayout topAll;
    
    @BindView(R.id.resaut_status_img)
    ImageView resautStatusImg;
    
    @BindView(R.id.resaut_status)
    LinearLayout resautStatus;
    
    @BindView(R.id.progressBar)
    ProgressBar progressBar;
    
    @BindView(R.id.loading_date)
    LinearLayout loadingDate;
    
    @BindView(R.id.listview)
    ListView listview;
    
    @BindView(R.id.address)
    TextView address;
    
    @BindView(R.id.dingdian_number)
    TextView dingdianNumber;
    
    @BindView(R.id.back)
    ImageView back;
    
    private int id;
    
    private ArrayList<MarketItem.ListBean> lists;
    
    private MarKetAdapter adapter;
    
    private MarketItem item;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mar_ket);
        ButterKnife.bind(this);
        id = getIntent().getIntExtra("id", -1);
        initView();
        initDate();
    }
    
    private void initView()
    {
        topText.setText("订单详情");
        if (lists == null)
        {
            lists = new ArrayList<>();
            adapter = new MarKetAdapter(this, lists, R.layout.market_adapter_item);
            listview.setAdapter(adapter);
        }
        else
            adapter.notifyDataSetChanged();
        if (item != null)
        {
            address.setText(item.getDeliverystatus());
            dingdianNumber.setText("订单号：" + item.getNumber());
        }
    }
    
    private void initDate()
    {
        loadingDate.setVisibility(View.VISIBLE);
        resautStatus.setVisibility(View.GONE);
        Observable<MarketDate> observable = RetrofitUtil.getInstance().getApiOfPhp().queryMarket(id);
        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(
            new Observer<MarketDate>()
            {
                @Override
                public void onSubscribe(Disposable d)
                {
                    disposables.add( d) ;
                }
                
                @Override
                public void onNext(MarketDate marketDate)
                {
                    if (marketDate.isFlag() && marketDate.getResult() != null
                        && marketDate.getResult().getList() != null && !marketDate.getResult().getList().isEmpty())
                    {
                        item = marketDate.getResult();
                        lists.clear();
                        lists.addAll(marketDate.getResult().getList());
                        initView();
                    }
                    else
                    {
                        resautStatus.setVisibility(View.VISIBLE);
                    }
                }
                
                @Override
                public void onError(Throwable e)
                {
                    loadingDate.setVisibility(View.GONE);
                    resautStatus.setVisibility(View.VISIBLE);
                    
                }
                
                @Override
                public void onComplete()
                {
                    loadingDate.setVisibility(View.GONE);
                }
            });
    }
    
    public static void startActivity(Context context, int id)
    {
        Intent intent = new Intent(context, MarKetActivity.class);
        intent.putExtra("id", id);
        context.startActivity(intent);
    }
    
    @OnClick(R.id.back)
    public void onViewClicked()
    {
        finish();
    }
}
